import 'package:flutter/material.dart';
import 'package:logger_util/logger_util.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var path = await LogPath.getLoggerDir();
  print('>>>>>>>>>>>$path');
  await initLogger(
      filter: null,
      isConsole: false,
      fileHeadNotifier: Normal.instance.valueNotifier);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class Normal {
  // 工厂模式
  factory Normal() => _getInstance();

  static Normal get instance => _getInstance();
  static Normal? _instance;

  static Normal _getInstance() {
    if (_instance == null) {
      _instance = Normal._internal();
    }
    return _instance!;
  }

  ValueNotifier<String> get valueNotifier => _valueNotifier;
  late ValueNotifier<String> _valueNotifier;

  Normal._internal() {
    _valueNotifier = ValueNotifier('测试');
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({super.key, required this.title});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  late String _longStr;
  late LogObject _logObject;

  void _incrementCounter() {
    setState(() {
      print('开始logger输出');
      Normal.instance.valueNotifier.value = '新的头文件$_counter';
      //批量插入测试
      for (var i = 0; i < 10; i++) {
        var indexSub = '$_counter--$i';
        LogUtil.logV('verbose $indexSub');
        LogUtil.logD('debug $indexSub');
        LogUtil.logI('info $indexSub');
        LogUtil.logW('warning $indexSub');
        LogUtil.logE('error $indexSub');
        LogUtil.logWTF('what the funck$_longStr $indexSub');
        LogUtil.logI('测试分文件存储info0 $indexSub', fileSubName: 'tvlian接');
        LogUtil.logI('测试分文件存储info1 $indexSub', fileSubName: '电视连接');
        LogUtil.logI('测试分文件存储info2 $indexSub', fileSubName: 'tv23455');
        LogUtil.logI('测试分文件存储info2', fileSubName: 'tv23455', tag: '随便标记');

        _logObject.logV('verbose $indexSub');
        _logObject.logD('debug $indexSub');
        _logObject.logI('info $indexSub');
        _logObject.logW('warning $indexSub');
        _logObject.logE('error $indexSub');
        _logObject.logWTF('what the funck$_longStr $indexSub');
        _logObject.log('测试分文件存储info2 $indexSub', level: Level.debug);
        _logObject.logI('测试分文件存储info2', tag: '随便标记');
      }

      LogPath.getLoggerZip(
              startDate: DateTime.fromMillisecondsSinceEpoch(1620631316000),
              endDate: DateTime.fromMillisecondsSinceEpoch(1620717716000))
          .then((value) {
        print('/////////////////$value');
      });
      _counter++;
    });
  }

  @override
  void initState() {
    _longStr = '';
    //长log测试
    for (var i = 0; i < 1000; i++) {
      _longStr += '记录着当前的进度记录着当前的进度记录着当前的进度记录着当前的进度记录着当前的进度当前进度：$i';
    }
    _logObject = LogObject.instance..fileSubName = '统一设定的文件名';
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
